var fs = require("fs");
var path = require("path");
var xlsx = require("node-xlsx");

var obj = xlsx.parse(path.relative(__dirname, "1.xlsx"));
const xlsx_data = [["项目名称", "车牌号", "运输记录（商品名称）", "商品编号", "商品名称", "单位", "数量", "运输起始地", "运输目的地（垃圾，土，水泥块，砖渣）"]];

exec(obj[0]["data"]);
function exec(data) {


	data.forEach((item, index) => {
		if (index && item.length == 7) {
			// 0，2，7，8
			console.log("item", item);
			let vehicle = item[1].split("/");
			let origin = item[5].split("/");
			let destination = item[6].split("/");
			let name = item[0];
			vehicle.forEach((el, index) => {
				xlsx_data.push([name, "",  el,'','','','',origin[index],destination[index]]);
			});
		}
	});
}

const options = {
	"!cols": [{ wch: 30 }, { wch: 35 }, { wch: 35 }, { wch: 35 }, { wch: 35 }, { wch: 35 }, { wch: 35 }, { wch: 105 }],
};
// 生成 buffer
const buffer = xlsx.build([{ name: "jsliang", data: xlsx_data }], options); // 拿到文件 buffer

// 写入文件
fs.writeFileSync(`${__dirname}/导出文件.xlsx`, Buffer.from(buffer));
